#ifndef RANDOM_H
#define RANDOM_H

#include <cstdlib>
#include <cmath>
#include <ctime>

inline int random(int a, int b) {
    return rand() % (b - a + 1) + a;
}

bool chance(float rate) {
    if ((float)rand() / RAND_MAX <= rate)
        return true;
    return false;
}

float normal(float u = 0.0f, float s = 1.0f)
{
    static float V1, V2, S;
    static int phase = 0;
    float X;

    if (phase == 0) {
        do {
            float U1 = (float)rand() / RAND_MAX;
            float U2 = (float)rand() / RAND_MAX;
            V1 = 2 * U1 - 1;
            V2 = 2 * U2 - 1;
            S = V1 * V1 + V2 * V2;
        } while (S >= 1 || S == 0);
        X = V1 * sqrt(-2 * log(S) / S);
    } else
        X = V2 * sqrt(-2 * log(S) / S);

    phase = 1 - phase;

    float rst = s * X + u;
    return rst;
}

#endif